<template>
  <el-main>
    <el-form :model="params" ref="searchForm" size="small">
        <el-button v-if="userType == '2'" type="primary" @click="addIntroduction">新建</el-button>
    </el-form>

    <!-- <el-table
      :height="tableHeight"
      :data="tableList"
      row-key="menuId"
      :tree-props="{ children: 'children' }"
      border
      stripe
      default-expand-all
    > -->
    <el-table :data="tableList" class="tableAuto" empty-text="暂无数据" size="small" :height="tableHeight"
      table-layout="auto" border fit stripe>
      <el-table-column prop="name" label="实验名称"> </el-table-column>
      <el-table-column prop="className" v-if="userType == '2'" label="所属班级"></el-table-column>
      <el-table-column prop="difficulties" label="实验难点"></el-table-column>
      <el-table-column label="操作" v-if="userType == '2'" width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="editIntroduction(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete"
            @click="deleteIntroduction(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page.sync="params.currentPage"
      :page-sizes="[10, 20, 40, 80, 100]" :page-size="params.pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="params.total" background>
    </el-pagination>

    <sys-dialog :title="dialog.title" :visible="dialog.visible" :width="dialog.width" :height="dialog.height"
      @onClose="onClose" @onConfirm="onConfirm">
      <div slot="content">
        <el-form :model="addDifficulties" ref="addForm" :rules="rules" label-width="80px" :inline="false" size="small">
          <el-form-item prop="name" label="实验名称">
            <el-input v-model="addDifficulties.name"></el-input>
          </el-form-item>
          <el-form-item prop="className" label="班级名称">
            <el-select v-model="addDifficulties.classId"  placeholder="请选择">
              <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="background" label="实验难点">
            <el-input type="textarea" v-model="addDifficulties.difficulties" :rows="5"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </sys-dialog>
  </el-main>
</template>

<script>
import {
  getStudentPreviewApi,
  getTeacherPreviewApi,
  updatePreviewApi,
  addPreviewApi,
  getClassListApi,
  deletePreviewApi
} from "@/api/preview";
import SysDialog from "@/components/System/SysDialog.vue";
export default {
  name: "Preview",
  components: {
    SysDialog,
  },
  data() {
    return {
      tableList: [],
      parentList: [],
      addDifficulties: {
        id: 0,
        tno: 0,
        classId: 0,
        name: "",
        className: "",
        difficulties:"",
      },
      params: {
        total: 0,
      },
      dialog: {
        title: "",
        visible: false,
        width: 550,
        height: 320,
      },
      options: [],
      tableHeight: 0,
      rules: {
        name: [
          {
            required: blur,
            trigger: "change",
            message: "请输入实验名称!",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.userType = localStorage.getItem("type");
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 200;
    });
    this.getList()
  },

  methods: {
    async getList() { 
      // 获取班级
      getClassListApi().then((res) => {
        this.options = res.data;
        // console.log(res);
        // console.log(this.options);
      })
      //获取实验列表
      if (this.userType == '1') {
        var res = await getStudentPreviewApi(this.params);
        var success = res.success
      } else if (this.userType == '2') {
        var res = await getTeacherPreviewApi(this.params);
        var success = res.success
      }
      if (success) {
        this.tableList = res.data;
        this.params.total = res.data.total;
      }
    },
    //新增
    addIntroduction() {
      this.$resetForm("addForm", this.addDifficulties);
      this.addDifficulties.typeNo = "0";
      this.dialog.title = "新增实验介绍";
      this.dialog.visible = true;
    },
    // 编辑
    editIntroduction(row) {
      this.dialog.title = "编辑内容预习";
      //清空表单数据
      this.$resetForm("addForm", this.addDifficulties);
      this.addDifficulties.typeNo = "1";
      // 编辑回显
      this.$objCoppy(row, this.addDifficulties);
      this.dialog.visible = true;
    },
    // 删除
    async deleteIntroduction(row) {
      let confirm = await this.$myconfirm("确定删除该实验介绍吗？");
      if (confirm) {
        let res = await deletePreviewApi({ id: row.id });
        //删除成功，刷新列表
        if (res.success) {
          this.getList();
          this.$message.success(res.msg);
        }
      }
    },
    //确认对话框
    onConfirm() {
      this.$refs.addForm.validate(async (valied) => {
        if (valied) {
          var res = null;
          if (this.addDifficulties.typeNo == "0") {
            console.log(this.addDifficulties);
            res = await addPreviewApi(this.addDifficulties);
          } else {
            console.log(this.addDifficulties);
            res = await updatePreviewApi(this.addDifficulties);
          }
          if (res.success) {
            //请求成功 刷新列表
            this.getList();
            this.dialog.visible = false;
            this.$message.success(res.msg);
          }
        }
      });
    },
    //关闭对话框
    onClose() {
      this.dialog.visible = false;
    },
    //页容量改变时触发
    sizeChange(val) {
      this.params.pageSize = val;
      this.getList();
    },
    //当前页数改变时触发
    currentChange(val) {
      this.params.currentPage = val;
      this.getList();
    },
    // 搜索
    searchUser() {
      this.getList();
    },
    // 重置表格
    resetUser() {
      this.params.name = "";
      this.params.phoneNumber = "";
      this.params.idCard = "";
      this.getList();
    },
  },
};
</script>
  
  <style>
</style>